package com.fosun.spr.core.exception;

import com.fosun.spr.core.message.IResultMessage;


/**
 * 
 * @Title: ServiceException.java
 * @Description: 自定义异常类
 * @author zhangcheng
 * @version V1.0
 * 
 */
public class ServiceException extends RuntimeException implements
		IResultMessage {
	public static String CODE_ERR = "-1";

	/** 版本序列号 **/
	private static final long serialVersionUID = 2765439943243341557L;

	/** 错误返回码 **/
	private String code;

	/** 错误描述 **/
	private String message;

	/** 默认构造函数 **/
	public ServiceException() {
	}

	/**
	 * 构造函数
	 * 
	 * @param result
	 *            错误返回码
	 */
	public ServiceException(IResultMessage result) {
		super(result.getMessage());
		this.code = result.getCode();
		this.message = result.getMessage();
	}
	public ServiceException(String code,String message) {
		this.code = code;
		this.message = message;
	}
	public ServiceException(IResultMessage result,String msg) {
		super(result.getMessage());
		this.code = result.getCode();
		this.message = result.getMessage()+"["+msg+"]";
	}	

	public ServiceException(String msg) {
		super(msg);
		this.message = msg;
	}

	/**
	 * 构造函数
	 * 
	 * @param result
	 *            错误返回码
	 * @param cause
	 *            引发异常的原因
	 */
	public ServiceException(IResultMessage result, Throwable cause) {
		super(result.getMessage(), cause);
		this.code = result.getCode();
		this.message = result.getMessage();
	}

	/**
	 * 获取错误返回码
	 * 
	 * @return 错误返回码
	 */
	public String getCode() {
		return code;
	}

	/**
	 * 设置错误返回码
	 * 
	 * @param code
	 *            错误返回码
	 */
	public void setCode(String code) {
		this.code = code;
	}

	/**
	 * 获取错误描述
	 * 
	 * @return 错误描述
	 */
	public String getMessage() {
		return this.message;
	}

	/**
	 * 设置错误描述
	 * 
	 * @param message
	 *            错误描述
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	@Override
	public String toString() {
		return getClass().getSimpleName() + "[" + code + "] "
				+ this.getMessage();
	}
}
